import requests
import config


class DriverRequests:
    def __init__(self, is_ssl, i_proxies):
        self.proxies = i_proxies
        if self.proxies:
            print('--------------------需要启动代理', self.proxies)
            self.proxies_conf = {
                'https': '127.0.0.1:8888'
            }
            print(self.proxies_conf)
        else:
            self.proxies_conf = None
        self.Is_ssl = is_ssl
        requests.packages.urllib3.disable_warnings()
        self.d_request = requests.session()

    def http_get_by_json(self, url):
        response = self.d_request.get(url, headers=config.Headers, verify=self.Is_ssl, proxies=self.proxies_conf)
        return response.json()
        # return Response.content.decode('UTF-8')

    def http_post_by_json(self, url, post_data):
        response = self.d_request.post(url, headers=config.Headers, data=post_data, verify=self.Is_ssl, proxies=self.proxies_conf)
        return response.json()
        # return Response.content.decode('UTF-8')

    def http_download_file(self, url, filename):
        response = self.d_request.get(url, headers=config.Headers, verify=self.Is_ssl)
        response.raise_for_status()
        with open(filename, 'wb') as obj:
            obj.write(response.content)
